﻿@using SmartStore.Admin.Models.DataExchange;
@using SmartStore.Web.Framework;
@model ExportFileDetailsModel
@{
	ViewBag.DownloadString = T("Common.Download").Text;
	ViewBag.AllStoresString = T("Admin.Common.StoresAll").Text;
}
@if (Model.ExportFiles.Any() || Model.PublicFiles.Any())
{
	if (Model.ExportFiles.Any())
	{
		<div class="mb-5">
			@foreach (var grp in Model.ExportFiles.OrderBy(x => x.DisplayOrder).GroupBy(x => x.DisplayOrder))
			{
				foreach (var file in grp)
				{
					<div class="py-1">
						<span class="badge @(file.RelatedType.HasValue ? "badge-secondary" : "badge-info")">
							@Html.IconForFileExtension(file.FileExtension, false)
							@(file.Label.HasValue() ? file.Label : file.FileExtension.EmptyNull().ToLower().TrimStart('.'))
						</span>
						<a href="@Url.Action("DownloadExportFile", new { id = Model.Id, name = file.FileName, isDeployment = Model.IsForDeployment })" title="@ViewBag.DownloadString" download>
							@file.FileRootPath
						</a>
					</div>
				}
			}
		</div>
	}
	<div>	
		@foreach (var grp in Model.PublicFiles.OrderBy(x => x.StoreId).GroupBy(x => x.StoreId))
		{
			var storeName = grp.First().StoreName;
			<div class="mb-3">
				<div>
					<span class="fs-h6">@T("Admin.Common.PublicFiles")</span>
					<span class="smaller">(@(storeName.HasValue() ? storeName : ViewBag.AllStoresString))</span>
				</div>

				@foreach (var file in grp)
				{
					<div class="py-1 d-flex copy-to-clipboard-container">
						<div>
							<span class="badge @(file.RelatedType.HasValue ? "badge-secondary" : "badge-success")">
								@Html.IconForFileExtension(file.FileExtension, false)
								@(file.Label.HasValue() ? file.Label : file.FileExtension.EmptyNull().ToLower().TrimStart('.'))
							</span>
							<a href="@file.FileUrl" title="@ViewBag.DownloadString" download>@file.FileUrl</a>
						</div>
						<div class="ml-auto">
							<button type="button" class="btn btn-sm btn-secondary copy-to-clipboard">
								<i class="far fa-clipboard"></i>
							</button>
						</div>
					</div>
				}
			</div>
		}
	</div>
}
else
{
	<div class="alert alert-info">@T("Common.NoFilesAvailable")</div>
}

<script type="text/javascript">
	jQuery(document).ready(function () {

		// copy url to clipboard button
		$('button.copy-to-clipboard').tooltip({
			placement: 'bottom',
			trigger: 'hover',
			title: @T("Common.CopyToClipboard").JsText
		}).click(function () {
			var self = $(this),
				url = self.closest('.copy-to-clipboard-container').find('a').attr('href');

			var succeeded = window.copyTextToClipboard(url);

			self.attr('data-original-title', succeeded ? @T("Common.CopyToClipboard.Succeeded").JsText : @T("Common.CopyToClipboard.Failed").JsText)
				.tooltip('show');

			setTimeout(function () {
				self.attr('data-original-title', '@T("Common.CopyToClipboard").JsText').tooltip('hide');
			}, 2000);
		});
	});
</script>